import asyncio
from TikTokApi import TikTokApi
import json

async def extract_tiktok_video(url: str, timeout_seconds=120) -> str:
    async with TikTokApi() as api:
        # ✅ 必须先创建 session，否则调用会报错
        await api.create_sessions(num_sessions=1, headless=True)

        video = api.video(url=url)
        data = await video.info()

        # 视频 ID
        aweme_id = data.get("id")
        print("视频 ID:", aweme_id)

        # 作者
        author = data["author"].get("unique_id")
        print("作者:", author)

        # # 无水印视频地址（临时直链）
        # video_url = data["video"]["play_addr"]["url_list"][0]
        # print("无水印地址:", video_url)
        # 打印整个返回内容
        print("==== 返回的完整 JSON ====")
        print(json.dumps(data, indent=2, ensure_ascii=False))

        return url


async def main():
    url = "https://www.tiktok.com/@scout2015/video/6718335390845095173"
    video_url = await extract_tiktok_video(url)
    print("提取结果:", video_url)

asyncio.run(main())
